﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AutoGear.BLL;
using System.Web.Script.Services;
using ECommerce.Library.DataStructure;
using Entity;

/// <summary>
/// Summary description for CartService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class CartService : System.Web.Services.WebService {

    public CartService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod (EnableSession =true)]
    [ScriptMethod(UseHttpGet = true)]
    public List<ShopItem> GetAllProducts()
    {
        List<ShopItem> lst = new List<ShopItem>();
        
        ShoppingCart shoppingcart = AutoGearSessionManager.ShoppingCart;
        //ShopItem e = new ShopItem {productId=309, productName = "Ashraful", productQuantity = 2, productTotalPrice = "254" };
        //lst.Add(e);
        //e = new ShopItem { productId = 310, productName = "Bill", productQuantity = 3, productTotalPrice = "545" };
        //lst.Add(e);

        for (int i = 0; i < shoppingcart.CartItems.Count; i++)
        {
            ShopItem items = new ShopItem();
            items.productId = shoppingcart.CartItems[i].Product.ProductId;
            items.productName = shoppingcart.CartItems[i].Product.ProductName;
            items.productQuantity = shoppingcart.CartItems[i].Quantity;
            if (shoppingcart.CartItems[i].Product.StandardCost > 0)
            {
                items.productTotalPrice = ((shoppingcart.CartItems[i].Product.StandardCost + shoppingcart.CartItems[i].Product.AdditionalPrice) * shoppingcart.CartItems[i].Quantity).ToString();
            }
            else
            {
                items.productTotalPrice = ((shoppingcart.CartItems[i].Product.ProductPrice + shoppingcart.CartItems[i].Product.AdditionalPrice) * shoppingcart.CartItems[i].Quantity).ToString();
            }
            lst.Add(items);
        }
        return lst;
    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public List<ShopItem> UpdateProductQuantity(string productId, string quantity)
    {
        List<ShopItem> lst = new List<ShopItem>();

        ShoppingCart shoppingcart = AutoGearSessionManager.ShoppingCart;
        for (int count = 0; count < shoppingcart.CartItems.Count; count++)
        {
            if (shoppingcart.CartItems[count].Product.ProductId == Convert.ToInt32(productId))
            {
                shoppingcart.CartItems[count].Quantity = Convert.ToInt32(quantity);
            }
        }
        AutoGearSessionManager.ShoppingCart = shoppingcart;
        ShoppingCart tempshop = AutoGearSessionManager.ShoppingCart;

        for (int i = 0; i < tempshop.CartItems.Count; i++)
        {
            ShopItem items = new ShopItem();
            items.productId = tempshop.CartItems[i].Product.ProductId;
            items.productName = tempshop.CartItems[i].Product.ProductName;
            items.productQuantity = tempshop.CartItems[i].Quantity;
            if (tempshop.CartItems[i].Product.StandardCost > 0)
            {
                items.productTotalPrice = ((tempshop.CartItems[i].Product.StandardCost + tempshop.CartItems[i].Product.AdditionalPrice) * tempshop.CartItems[i].Quantity).ToString();
            }
            else
            {
                items.productTotalPrice = ((tempshop.CartItems[i].Product.ProductPrice + tempshop.CartItems[i].Product.AdditionalPrice) * tempshop.CartItems[i].Quantity).ToString();
            }
            lst.Add(items);
        }
        return lst;
    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public string UpdateEmployee(List<ShippingInfo> data)
    {
        Session["OrderAmount"] = null;
        string message = string.Empty;
        
        ShoppingCart shoppingcart = AutoGearSessionManager.ShoppingCart;
        List<MasterInfo> masterCart = new List<MasterInfo>();
        for (int count = 0; count < shoppingcart.CartItems.Count; count++) 
        {
            MasterInfo master = new MasterInfo();
            master.productId = shoppingcart.CartItems[count].Product.ProductId;
            master.productQuantity = shoppingcart.CartItems[count].Quantity;
            masterCart.Add(master);
        }


        ShoppingCart tempshop = AutoGearSessionManager.ShoppingCart;
        AutoGearSessionManager.ShoppingCart = null;
        shoppingcart = AutoGearSessionManager.ShoppingCart;
        MultipleShipAdd multishipadd = new MultipleShipAdd();

        if (data.Count > 0)
        {
            for (int i = 0; i < data.Count; i++)
            {

                AutoGear.BLL.Address shipAddress = new AutoGear.BLL.Address();
                shipAddress.FirstName = data[i].firstName;
                shipAddress.LastName = data[i].lastName;
                shipAddress.AddressLine1 = data[i].add1;
                shipAddress.AddressLine2 = data[i].add2;
                shipAddress.EmailAddress = data[i].email;
                shipAddress.TelephoneNumber = data[i].phone;
                shipAddress.City = data[i].city;
                shipAddress.AddressState = data[i].state;
                shipAddress.ZipCode = data[i].zip;
                shipAddress.AddressCountry = data[i].country;
                bool isResedent;
                decimal _totalPrice = 0;
                string shippingMethod, giftCardMessage, requestDate, addressBookId;

                if (data[i].resident == true)
                    isResedent = true;
                else
                    isResedent = false;
                shippingMethod = data[i].shipmethod;
                giftCardMessage = data[i].giftMsg;
                requestDate = data[i].Shipdate;
                addressBookId = data[i].AddressBookId;

                for (int j = 0; j < data[i].products.Count; j++)
                {
                    Product product = Product.Load(Convert.ToInt32(data[i].products[j].productId));

                    _totalPrice += Convert.ToDecimal((data[i].products[j].productQuantity * (product.ProductPrice + product.AdditionalPrice)));
                }

                
                for (int j = 0; j < data[i].products.Count; j++)
                {
                    Product product = Product.Load(Convert.ToInt32(data[i].products[j].productId));
                    CartItem cartItem = new CartItem(product, data[i].products[j].productQuantity, string.Empty, string.Empty);
                    shoppingcart.AddCartItem(cartItem);
                    Additional productadd = new Additional();
                    productadd.ProductId = data[i].products[j].productId;
                    productadd.ProductName = data[i].products[j].productName;
                    productadd.ProductQuantity = data[i].products[j].productQuantity;
                    productadd.ProductPrice = product.ProductPrice + product.AdditionalPrice;
                    multishipadd = new MultipleShipAdd(shipAddress, productadd, isResedent, shippingMethod, giftCardMessage,requestDate,addressBookId,Convert.ToDouble(_totalPrice));
                    shoppingcart.AddMultipleShipAdd(multishipadd);
                }
                if (Session["minAmount"] != null)
                {
                    if (Convert.ToDouble(_totalPrice) < SiteSettings.MinimumOrderAmount)
                    {
                        Session["OrderAmount"] = "You have not met the minimum order requirement of $" + SiteSettings.MinimumOrderAmount.ToString() + " for each address";
                    }
                }
            }

        }
        for (int comp = 0; comp < masterCart.Count; comp++)
        {
            for (int com2 = 0; com2 < shoppingcart.CartItems.Count; com2++)
            {
                if (masterCart[comp].productId == shoppingcart.CartItems[com2].Product.ProductId)
                {
                    if (masterCart[comp].productQuantity == shoppingcart.CartItems[com2].Quantity)
                    {
                        message = "Success";
                    }
                    else if (masterCart[comp].productQuantity > shoppingcart.CartItems[com2].Quantity)
                    {
                        message = "Product quantity distribution is still less than total quantity";
                        break;
                    }
                    else if (masterCart[comp].productQuantity < shoppingcart.CartItems[com2].Quantity)
                    {
                        //message = "Product quantity distribution is greater than total product quantity. Do you want to proceed?";
                        //break;
                        message = "Success";
                    }
                }
            }
        }
        if (message != "Success")
        {
            //AutoGearSessionManager.ShoppingCart = null;
            AutoGearSessionManager.ShoppingCart = tempshop;
        }
        else 
        {
            if (Session["OrderAmount"] != null)
            {
                message = Session["OrderAmount"].ToString();
                AutoGearSessionManager.ShoppingCart = shoppingcart;
            }
            else 
            {
                AutoGearSessionManager.ShoppingCart = shoppingcart;
            }
            
        }
        return message;
    }

    public class ShopItem
    {
        public int productId { get; set; }
        public string productName { get; set; }
        public int productQuantity { get; set; }
        public string productTotalPrice { get; set; }

    }
    public class ShippingInfo
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string phone { get; set; }
        public string email { get; set; }
        public string add1 { get; set; }
        public string add2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
        public string country { get; set; }
        public bool resident { get; set; }
        public string shipmethod { get; set; }
        public string giftMsg { get; set; }
        public string Shipdate { get; set; }
        public string AddressBookId { get; set; }
        public List<ShopItem> products { get; set; }
    }
    public class MasterInfo
    {
        public int productId { get; set; }
        public int productQuantity { get; set; }
    }
    
}
